﻿//GB/Z 29014.3 ISYC 307-08-多排可转位刀片非中心切削柄连接V型立铣刀

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{
    class ISYC_307_08_F76
    {
        public static void ISYC_307_F76_H(Dictionary<string, Object> data)
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中性线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //基本参数-------------------------------------------------------------------------------------------------
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            //赋值参数--------------------------------------------------------------------------------------------------
            double scale = 1;
            double OAL = 40.26 * scale;
            double LPR = 25.6 * scale;
            double APMX = 14.4 * scale;
            double DC = 8.32 * scale;
            double LH = 18.48 * scale;
            double LU = 15.85 * scale;
            double BD = 7.44 * scale;
            double DCX = 12.95 * scale;
            double DCONMS = 7.95 * scale;
            double LS = 19.47 * scale;
            double BHTA = 10;

            //点---------------------------------------------------------------------------------------------
            //第一部分
            //轮廓
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X + OAL, v1.Y);
            Vector2 v4 = new Vector2(v1.X + (0.47 / 19.47) * LS, v1.Y + DCONMS / 2.0);
            Vector2 v3 = fun.Inters(v1, fun.Polar(v1, 90 * pi / 180, DCONMS), v4, fun.Polar(v4, -135 * pi / 180, OAL));
            Vector2 v5 = new Vector2(v2.X - LH, v2.Y + DCONMS / 2.0);
            Vector2 v6 = new Vector2(v5.X, v5.Y + (1 / 8.32) * DC);
            Vector2 v7 = new Vector2(v5.X, v5.Y + (3 / 8.32) * DC);
            double a = LH / Math.Cos(BHTA * pi / 180);//v7与v15之间的长度
            Vector2 v8 = fun.Polar(v7, -BHTA * pi / 180, (7.29 / 18.7) * a);
            double b = (2.58 / 8.45) * (DC / Math.Cos(BHTA * pi / 180));//上方刀片宽度
            double c1 = (3.49 / 18.7) * a ;//上方左刀片长度
            double c2 = (5.67 / 18.7) * a ;//上方右刀片长度
            Vector2 v9 = fun.Polar(v8, (90 - BHTA) * pi / 180, (0.44 / 2.58) * b);
            Vector2 v10 = fun.Polar(v9, -BHTA * pi / 180, c1);
            Vector2 v11 = fun.Polar(v10, -(90 + BHTA) * pi / 180, b);
            Vector2 v12 = fun.Polar(v9, -(90 + BHTA) * pi / 180, b);
            Vector2 v13 = fun.Inters(v8, fun.Polar(v8, -BHTA * pi / 180, OAL), v10, v11);
            Vector2 v14 = fun.Polar(v13, -BHTA * pi / 180, (2.25 / 18.7) * a);
            Vector2 v15 = new Vector2(v1.X + OAL, v2.Y + DC / 2.0);
            Vector2 v16= fun.Polar(v14, (90 - BHTA) * pi / 180, (0.44 / 2.58) * b);
            Vector2 v17 = fun.Polar(v16, -(90 + BHTA) * pi / 180, b);
            Vector2 v18 = fun.Polar(v15, -(90 + BHTA) * pi / 180, b);
            Vector2 v19 = fun.Inters(v18, fun.Polar(v18, -90 * pi / 180, OAL), v1, v2);
            Vector2 v20 = fun.Polar(v7, -BHTA * pi / 180, (2.68 / 18.7) * a);
            Vector2 v21 = new Vector2(v18.X, -v18.Y);
            Vector2 v23 = new Vector2(v7.X, -v7.Y);
            Vector2 v22 = fun.Inters(v23, fun.Polar(v23, BHTA * pi / 180, OAL), v21, fun.Polar(v21, -90 * pi / 180, OAL));
            Vector2 v24 = new Vector2(v6.X, -v6.Y);
            Vector2 v25 = new Vector2(v5.X, -v5.Y);
            Vector2 v26 = new Vector2(v4.X, -v4.Y);
            Vector2 v27 = new Vector2(v3.X, -v3.Y);
            Vector2 v28 = new Vector2(v18.X, -v18.Y);
            Vector2 v29 = new Vector2(v15.X, -v15.Y);
            Vector2 v30 = fun.Polar(v29, -(180 - BHTA) * pi / 180, (3.67 / 18.7) * a);
            Vector2 v31 = fun.Inters(v30, fun.Polar(v30, (90 + BHTA) * pi / 180, OAL), v23, v22);
            Vector2 v32 = fun.Polar(v31, -(180 - BHTA) * pi / 180, (2 / 18.7) * a);
            Vector2 v33 = fun.Polar(v30, -(180 - BHTA) * pi / 180, (2 / 18.7) * a);
            Vector2 v34 = fun.Polar(v33, -(180 - BHTA) * pi / 180, (3.48 / 18.7) * a);
            Vector2 v35 = fun.Polar(v32, -(180 - BHTA) * pi / 180, (3.48 / 18.7) * a);
            Vector2 v36 = fun.Polar(v35, -(180 - BHTA) * pi / 180, (1.84 / 18.7) * a);
            Vector2 v37 = fun.Polar(v34, -(180 - BHTA) * pi / 180, (1.84 / 18.7) * a);
            Vector2 v38 = fun.Polar(v37, -(180 - BHTA) * pi / 180, (3.63 / 18.7) * a);
            Vector2 v39 = fun.Polar(v36, -(180 - BHTA) * pi / 180, (3.63 / 18.7) * a);
            Vector2 v40 = new Vector2(v4.X + (19 / 19.47) * LS, v4.Y);
            Vector2 v41 = new Vector2(v40.X, -v40.Y);
            Vector2 v42 = fun.Inters(v9, v11, v10, v12);
            Vector2 v43 = fun.Inters(v15, v17, v16, v18);
            Vector2 v44 = new Vector2(v1.X + (14.66 / 19.47) * LS, v1.Y);
            Vector2 v45 = new Vector2(v44.X, v44.Y + (7.27 / 12.95) * DCX);
            Vector2 v46 = new Vector2(v45.X - (1.84 / 19.47) * LS, v45.Y + (1.54 / 12.95) * DCX);
            Vector2 v47 = new Vector2(v46.X - (3.44 / 19.47) * LS, v46.Y);
            Vector2 v48 = new Vector2(v45.X, -v45.Y);
            Vector2 v49 = new Vector2(v46.X, -v46.Y);
            Vector2 v50 = new Vector2(v47.X, -v47.Y);
            Vector2 v51 = new Vector2(v26.X - (2.77 / 19.47) * LS, v26.Y - (4.2 / 7.95) * DCONMS);
            Vector2 v52 = new Vector2(v15.X + (8.28 / 40.26) * OAL, v15.Y + (14.52 / 12.95) * DCX);
            Vector2 v53 = new Vector2(v4.X + (9.81 / 19.47) * LS, v4.Y);

            //第二部分
            //中心线
            Vector2 v54 = new Vector2(v2.X + (22.31 / 40.26) * OAL, v2.Y);
            Vector2 v55 = new Vector2(v54.X + (15.81 / 40.26) * OAL, v54.Y);
            Vector2 v56 = new Vector2(v54.X + fun.CalDistance(v54, v55) / 2.0, v2.Y + (7.88 / 12.95) * DCX);
            Vector2 v57 = new Vector2(v54.X + fun.CalDistance(v54, v55) / 2.0, v2.Y - (7.88 / 12.95) * DCX);

            //轮廓
            Vector2 v58 = new Vector2(v54.X + fun.CalDistance(v54, v55) / 2.0, v2.Y + DCX / 2.0);
            Vector2 center = new Vector2(v54.X + fun.CalDistance(v54, v55) / 2.0, v2.Y);
            Vector2 v59 = fun.Inters1(v18, fun.Polar(v18, 0 * pi / 180, OAL), new Arc(center, DC / 2.0, 0, 360), 0);
            Vector2 v60 = new Vector2(v59.X + (2.25 / 8.32) * DC, v59.Y);
            Vector2 v61 = fun.Inters(v60, fun.Polar(v60, 28 * pi / 180, OAL), new Vector2(v58.X + (0.61 / 8.32) * DC, v58.Y), new Vector2(v58.X + (0.61 / 8.32) * DC, v58.Y - OAL));
            Vector2 v62 = fun.Inters(v58, fun.Polar(v58, -24 * pi / 180, OAL), new Vector2(v58.X + (0.61 / 8.32) * DC, v58.Y), new Vector2(v58.X + (0.61 / 8.32) * DC, v58.Y - OAL));
            Vector2 v63 = fun.Inters(v58, fun.Polar(v58, -90 * pi / 180, OAL), v60, v61);
            Vector2 v63_1 = fun.rotatePoint_Changed(center, v63, 180);
            Vector2 v64 = fun.Inters1(v62, v61, new Arc(center, DC / 2.0, 0, 360), 0);
            Vector2 v65 = fun.rotatePoint_Changed(center, v58, 180);
            Vector2 v66 = fun.rotatePoint_Changed(center, v59, 180);
            Vector2 v67 = fun.rotatePoint_Changed(center, v60, 180);
            Vector2 v68 = fun.rotatePoint_Changed(center, v61, 180);
            Vector2 v69 = fun.rotatePoint_Changed(center, v62, 180);
            Vector2 v70 = fun.rotatePoint_Changed(center, v64, 180);
            Vector2 v71 = fun.Inters1(v62, fun.Polar(v62, 1 * pi / 180, OAL), new Arc(center, fun.CalDistance(v7, v23) / 2.0, 0, 360), 1);
            Vector2 v72 = fun.rotatePoint_Changed(center, v71, 180);

            //作图部分-----------------------------------------------------------------------
            //中心线
            Line line1 = new Line(v1, v2);
            line1.Layer = centre;
            line1.Linetype = Linetype.Center;
            line1.LinetypeScale = 5;
            dxf.AddEntity(line1);

            Line line2 = new Line(v54, v55);
            line2.Layer = centre;
            line2.Linetype = Linetype.Center;
            line2.LinetypeScale = 5;
            dxf.AddEntity(line2);

            Line line3 = new Line(v56, v57);
            line3.Layer = centre;
            line3.Linetype = Linetype.Center;
            line3.LinetypeScale = 5;
            dxf.AddEntity(line3);

            //第一部分
            //轮廓
            LwPolyline polyline1 = new LwPolyline();
            polyline1.Vertexes.Add(new LwPolylineVertex(v1));
            polyline1.Vertexes.Add(new LwPolylineVertex(v3));
            polyline1.Vertexes.Add(new LwPolylineVertex(v4));
            polyline1.Vertexes.Add(new LwPolylineVertex(v5));//3
            polyline1.Vertexes.Add(new LwPolylineVertex(v6));
            polyline1.Vertexes.Add(new LwPolylineVertex(v7));
            polyline1.Vertexes.Add(new LwPolylineVertex(v8));

            //倒角
            var arc1 = Pus.Fillet4LwPline(polyline1, 3, 1);
            fun.PolyFill(dxf, arc1, 0.3, contour);

            LwPolyline polyline2 = new LwPolyline();
            polyline2.Vertexes.Add(new LwPolylineVertex(v9));
            polyline2.Vertexes.Add(new LwPolylineVertex(v10));
            polyline2.Vertexes.Add(new LwPolylineVertex(v11));
            polyline2.Vertexes.Add(new LwPolylineVertex(v12));
            polyline2.Vertexes.Add(new LwPolylineVertex(v9));
            fun.PolyFill(dxf, polyline2, 0.3, contour);

            LwPolyline polyline3 = new LwPolyline();
            polyline3.Vertexes.Add(new LwPolylineVertex(v15));
            polyline3.Vertexes.Add(new LwPolylineVertex(v16));
            polyline3.Vertexes.Add(new LwPolylineVertex(v17));
            polyline3.Vertexes.Add(new LwPolylineVertex(v18));
            polyline3.Vertexes.Add(new LwPolylineVertex(v15));
            fun.PolyFill(dxf, polyline3, 0.3, contour);

            LwPolyline polyline4 = new LwPolyline();
            polyline4.Vertexes.Add(new LwPolylineVertex(v18));
            polyline4.Vertexes.Add(new LwPolylineVertex(v22));
            polyline4.Vertexes.Add(new LwPolylineVertex(v23));
            polyline4.Vertexes.Add(new LwPolylineVertex(v24));
            polyline4.Vertexes.Add(new LwPolylineVertex(v25));//4
            polyline4.Vertexes.Add(new LwPolylineVertex(v26));
            polyline4.Vertexes.Add(new LwPolylineVertex(v27));
            polyline4.Vertexes.Add(new LwPolylineVertex(v1));

            //倒角
            var arc2 = Pus.Fillet4LwPline(polyline4, 4, 1);
            fun.PolyFill(dxf, arc2, 0.3, contour);

            LwPolyline polyline5 = new LwPolyline();
            polyline5.Vertexes.Add(new LwPolylineVertex(v28));
            polyline5.Vertexes.Add(new LwPolylineVertex(v29));
            polyline5.Vertexes.Add(new LwPolylineVertex(v30));
            polyline5.Vertexes.Add(new LwPolylineVertex(v31));
            fun.PolyFill(dxf, polyline5, 0.3, contour);

            LwPolyline polyline6 = new LwPolyline();
            polyline6.Vertexes.Add(new LwPolylineVertex(v32));
            polyline6.Vertexes.Add(new LwPolylineVertex(v33));
            polyline6.Vertexes.Add(new LwPolylineVertex(v34));
            polyline6.Vertexes.Add(new LwPolylineVertex(v35));
            fun.PolyFill(dxf, polyline6, 0.3, contour);

            LwPolyline polyline7 = new LwPolyline();
            polyline7.Vertexes.Add(new LwPolylineVertex(v36));
            polyline7.Vertexes.Add(new LwPolylineVertex(v37));
            polyline7.Vertexes.Add(new LwPolylineVertex(v38));
            polyline7.Vertexes.Add(new LwPolylineVertex(v39));
            fun.PolyFill(dxf, polyline7, 0.3, contour);

            LwPolyline polyline8 = new LwPolyline();
            polyline8.Vertexes.Add(new LwPolylineVertex(v4));
            polyline8.Vertexes.Add(new LwPolylineVertex(v26));
            fun.PolyFill(dxf, polyline8, 0.3, contour);

            LwPolyline polyline9 = new LwPolyline();
            polyline9.Vertexes.Add(new LwPolylineVertex(v40));
            polyline9.Vertexes.Add(new LwPolylineVertex(v41));
            fun.PolyFill(dxf, polyline9, 0.3, contour);

            LwPolyline polyline11 = new LwPolyline();
            polyline11.Vertexes.Add(new LwPolylineVertex(v6));
            polyline11.Vertexes.Add(new LwPolylineVertex(v24));
            fun.PolyFill(dxf, polyline11, 0.3, contour);

            LwPolyline polyline12 = new LwPolyline();
            polyline12.Vertexes.Add(new LwPolylineVertex(v20));
            polyline12.Vertexes.Add(new LwPolylineVertex(v19));//0
            polyline12.Vertexes[0].Bulge = Math.Tan(45 * pi / 180 / 4);
            fun.PolyFill(dxf, polyline12, 0.3, contour);

            LwPolyline pline13 = new LwPolyline();
            pline13.Vertexes.Add(new LwPolylineVertex(v13));
            pline13.Vertexes.Add(new LwPolylineVertex(v14));
            fun.PolyFill(dxf, pline13, 0.3, contour);

            fun.drawcircle(dxf, v42, (0.51 / 14.4) * APMX, 0.3, contour);
            fun.drawcircle(dxf, v43, (0.51 / 14.4) * APMX, 0.3, contour);

            //虚线
            LwPolyline polyline13 = new LwPolyline();
            polyline13.Vertexes.Add(new LwPolylineVertex(v47));
            polyline13.Vertexes.Add(new LwPolylineVertex(v46));
            polyline13.Vertexes.Add(new LwPolylineVertex(v45));
            polyline13.Vertexes.Add(new LwPolylineVertex(v48));
            polyline13.Vertexes.Add(new LwPolylineVertex(v49));
            polyline13.Vertexes.Add(new LwPolylineVertex(v50));
            polyline13.Linetype = Linetype.Dashed;
            polyline13.LinetypeScale = 5;
            polyline13.Layer = refin;
            dxf.AddEntity(polyline13);

            //第二部分
            //轮廓
            LwPolyline polyline14 = new LwPolyline();
            polyline14.Vertexes.Add(new LwPolylineVertex(v59));
            polyline14.Vertexes.Add(new LwPolylineVertex(v60));
            polyline14.Vertexes.Add(new LwPolylineVertex(v61));
            polyline14.Vertexes.Add(new LwPolylineVertex(v62));
            polyline14.Vertexes.Add(new LwPolylineVertex(v58));
            polyline14.Vertexes.Add(new LwPolylineVertex(v63));
            fun.PolyFill(dxf, polyline14, 0.3, contour);

            LwPolyline polyline15 = new LwPolyline();
            polyline15.Vertexes.Add(new LwPolylineVertex(v59));
            polyline15.Vertexes.Add(new LwPolylineVertex(v71));//0
            polyline15.Vertexes[0].Bulge = Math.Tan(-130 * pi / 180 / 4);
            fun.PolyFill(dxf, polyline15, 0.3, contour);

            LwPolyline polyline16 = new LwPolyline();
            polyline16.Vertexes.Add(new LwPolylineVertex(v62));
            polyline16.Vertexes.Add(new LwPolylineVertex(v71));//0
            polyline16.Vertexes[0].Bulge = Math.Tan(-19 * pi / 180 / 4);
            fun.PolyFill(dxf, polyline16, 0.3, contour);

            LwPolyline polyline17 = new LwPolyline();
            polyline17.Vertexes.Add(new LwPolylineVertex(v66));
            polyline17.Vertexes.Add(new LwPolylineVertex(v67));
            polyline17.Vertexes.Add(new LwPolylineVertex(v68));
            polyline17.Vertexes.Add(new LwPolylineVertex(v69));
            polyline17.Vertexes.Add(new LwPolylineVertex(v65));
            polyline17.Vertexes.Add(new LwPolylineVertex(v63_1));
            fun.PolyFill(dxf, polyline17, 0.3, contour);

            LwPolyline polyline18 = new LwPolyline();
            polyline18.Vertexes.Add(new LwPolylineVertex(v66));
            polyline18.Vertexes.Add(new LwPolylineVertex(v72));//0
            polyline18.Vertexes[0].Bulge = Math.Tan(-130 * pi / 180 / 4);
            fun.PolyFill(dxf, polyline18, 0.3, contour);

            LwPolyline polyline19 = new LwPolyline();
            polyline19.Vertexes.Add(new LwPolylineVertex(v69));
            polyline19.Vertexes.Add(new LwPolylineVertex(v72));//0
            polyline19.Vertexes[0].Bulge = Math.Tan(-19 * pi / 180 / 4);
            fun.PolyFill(dxf, polyline19, 0.3, contour);

            fun.drawcircle(dxf, center, fun.CalDistance(v7, v23) / 2.0, 0.3, contour);

            var arc3 = fun.DrawArc(v59, center, v70);
            fun.drawBArc(dxf, arc3, 0.3, contour);

            var arc4 = fun.DrawArc(v66, center, v64);
            fun.drawBArc(dxf, arc4, 0.3, contour);

            //标注---------------------------------------------------------------------------
            double height = 2 * scale;
            double arrowsize = 1 * scale;
            double exline = 0.5 * scale;

            //坐标系
            fun.Axis(dxf, v1, 180, 270, "", "", "PCS", 0.2 * scale, height * 0.5, 0);
            fun.Axis(dxf, v44, 180, 270, "", "", "MCS", 0.2 * scale, height * 0.5, 0);
            fun.Axis(dxf, v51, 180, 270, "Z", "Y", "X", 0.2 * scale, height * 0.5, 0);

            //DCONMS
            fun.dimrot1(dxf, v4, v26, 90, LS * 0.3, 0.1, height, arrowsize, exline, "DCONMS", "", "");

            //LS
            fun.dimrot1(dxf, v27, v25, 180, DCONMS * 2, 0.1, height, arrowsize, exline, "LS", "", "");

            //OAL
            fun.dimrot1(dxf, v3, v15, 0, DCONMS * 3, 0.1, height, arrowsize, exline, "OAL", "", "");

            //LPR
            fun.dimrot1(dxf, v45, v15, 0, DCONMS * 2, 0.1, height, arrowsize, exline, "LPR", "", "");

            //LU
            fun.dimrot1(dxf, v20, v15, 0, DCONMS * 1, 0.1, height, arrowsize, exline, "LU", "", "");

            //BD
            fun.dimrot1(dxf, v22, new Vector2(v22.X, -v22.Y), -90, OAL * 0.15, 0.1, height, arrowsize, exline, "BD", "", "");

            //DC
            fun.dimrot1(dxf, v15, v29, -90, OAL * 0.3, 0.1, height, arrowsize, exline, "DC", "", "");

            //DCX
            fun.dimrot1(dxf, v38, new Vector2(v22.X, -v38.Y), -90, OAL * 0.6, 0.1, height, arrowsize, exline, "DCX", "", "");

            //APMX
            fun.dimrot1(dxf, v38, v29, 180, DCONMS * 0.7, 0.1, height, arrowsize, exline, "APMX", "", "");

            //LH
            fun.dimrot1(dxf, v23, v29, 180, DCONMS * 2 - Math.Abs(v23.Y - v25.Y), 0.1, height, arrowsize, exline, "LH", "", "");

            //角度标注
            //BHTA
            fun.jdbz2(dxf, new Line(v7, fun.Polar(v7, (180 - BHTA) * pi / 180, 0.1)), new Line(v2, fun.Polar(v2, 180 * pi / 180, 0.1)), OAL * 0.8, height, arrowsize, exline, "BHTA", "", "");

            //箭头
            //FDP
            fun.jt1(dxf, v52, -90, 5, 1, "FDP", height);

            //CCMS
            fun.jt1(dxf, v53, 100, 5, 1, "CCMS", height);

            //字符
            fun.text(dxf, new Vector2(v56.X - (2.45 / 11.13) * DCONMS, v56.Y + (2.24 / 11.13) * DCONMS), height, "View A");
            
            //定义格式，根据路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}
